package com.q.tqb.fragment;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.reflect.TypeToken;
import com.q.tqb.R;
import com.q.tqb.activity.grab.GrabSpeaking2Activity;
import com.q.tqb.adapter.MineSendRedAdapter;
import com.q.tqb.base.AppConst;
import com.q.tqb.model.MineRedInfo;
import com.q.tqb.util.LogUtil;
import com.q.tqb.util.MD5Util;
import com.q.tqb.util.StringUtil;
import com.q.tqb.util.http.IResponse;
import com.q.tqb.util.http.OkHttpUtil;
import com.q.tqb.view.refresh.GRecyclerView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class MineSendRedFragment extends Fragment {


    @BindView(R.id.rv_mine_red)
    GRecyclerView mRvMineRed;

    private MineSendRedAdapter mMineRedAdapter;

    Unbinder unbinder;
    private int currentPageNo;

    private ArrayList<MineRedInfo> mGrabRedDatas;
    private int uid;

    public MineSendRedFragment() {
        // Required empty public constructor
    }


    public static MineSendRedFragment newInstance(int uid) {
        MineSendRedFragment fragment = new MineSendRedFragment();
        Bundle args = new Bundle();
        args.putInt("uid", uid);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            uid = getArguments().getInt("uid");
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_mine_red_envelope, container, false);
        unbinder = ButterKnife.bind(this, view);
        init();
        return view;
    }

    private void init() {
        mGrabRedDatas = new ArrayList<>();

        mRvMineRed.setLoadingMoreEnabled(true);
        mRvMineRed.setLoadingListener(new GRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                currentPageNo = 1;
                mRvMineRed.setNoMore(false);
                mRvMineRed.setLoadingMoreEnabled(true);
                getListData();
            }

            @Override
            public void onLoadMore() {
                currentPageNo++;
                getListData();
            }
        });

        mMineRedAdapter = new MineSendRedAdapter(getActivity(), mGrabRedDatas, 1, "tqb");

        mMineRedAdapter.setOnItemClickListener(new MineSendRedAdapter.OnItemClickListener() {
            @Override
            public void onClick(int position) {
                Intent intent;
                if (mGrabRedDatas.get(position).getPacket_type() == 2) {
                    //    intent = new Intent(getActivity(), GrabImageActivity.class);
                    intent = new Intent(getActivity(), GrabSpeaking2Activity.class);
                    intent.putExtra("rid", mGrabRedDatas.get(position).getRid());
                    startActivity(intent);
                } else if (mGrabRedDatas.get(position).getPacket_type() == 1) {
                    intent = new Intent(getActivity(), GrabSpeaking2Activity.class);
                    intent.putExtra("rid", mGrabRedDatas.get(position).getRid());
                    startActivity(intent);
                }else if (mGrabRedDatas.get(position).getPacket_type() == 3) {
                    intent = new Intent(getActivity(), GrabSpeaking2Activity.class);
                    intent.putExtra("rid", mGrabRedDatas.get(position).getRid());
                    intent.putExtra("type", 1);
                    startActivity(intent);
                }
//                else {
//                    intent = new Intent(getActivity(), GrabSpeaking1Activity.class);
//                    intent.putExtra("rid", mGrabRedDatas.get(position).getRid());
//                    intent.putExtra("type", 1);
//                    startActivity(intent);
//                }
            }

            @Override
            public void onLongClick(final int position) {
                AlertDialog.Builder normalDialog = new AlertDialog.Builder(getActivity());
                normalDialog.setTitle("温馨提示");
                normalDialog.setMessage("是否确认撤回红包？撤回红包将返" + mGrabRedDatas.get(position).getSurplus_money() + "元到您的余额");
                normalDialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.dismiss();
                        cancelRed(mGrabRedDatas.get(position).getRid());
                    }
                });
                normalDialog.setNegativeButton("取消", null);
                normalDialog.show();
            }
        });

        LinearLayoutManager linearLayoutManagert = new LinearLayoutManager(getActivity());
        mRvMineRed.setLayoutManager(linearLayoutManagert);
        mRvMineRed.setAdapter(mMineRedAdapter);
        mRvMineRed.setRefresh();
    }

    private void cancelRed(int rid) {
        Map<String, String> map = new HashMap<>();
        map.put("rid", rid + "");
        map.put("uid", uid + "");
        map.put("token", StringUtil.getToken(uid));
        LogUtil.i(map.toString());
        OkHttpUtil.getInstance().post(getActivity(), AppConst.URL_REVOKE_PACKET, map, new IResponse() {
            @Override
            public void onSuccess(JsonElement element) {
                LogUtil.i(element.toString());
                Toast.makeText(getActivity(), "红包撤回成功，剩余金额已返回您的余额！", Toast.LENGTH_SHORT).show();
                currentPageNo = 1;
                getListData();
            }

            @Override
            public void onFailure(int statusCode, String errorMsg) {
                LogUtil.i(errorMsg + "statusCode" + statusCode);
                Toast.makeText(getActivity(), errorMsg, Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void getListData() {
        Map<String, String> map = new HashMap<>();
        map.put("pageNo", currentPageNo + "");
        map.put("pageSize ", 10 + "");
        map.put("uid", uid + "");
        OkHttpUtil.getInstance().post(getActivity(), AppConst.URL_MINE_POST_PACKET_LIST, map, new IResponse() {
            @Override
            public void onSuccess(JsonElement element) {
                LogUtil.i(element.toString());
                Gson gson = new Gson();
                ArrayList<MineRedInfo> grabRedDatas = gson.fromJson(element, new TypeToken<ArrayList<MineRedInfo>>() {
                }.getType());
                if (currentPageNo == 1) {
                    mGrabRedDatas.clear();
                    mRvMineRed.refreshComplete();
                    if (grabRedDatas.size() < 10) {
                        mRvMineRed.setNoMore(true);
                    }
                } else {
                    if (grabRedDatas.size() < 10) {
                        mRvMineRed.setNoMore(true);
                    } else {
                        mRvMineRed.loadMoreComplete();
                    }
                }
                mGrabRedDatas.addAll(grabRedDatas);
                mMineRedAdapter.notifyDataSetChanged();
            }

            @Override
            public void onFailure(int statusCode, String errorMsg) {
                if (currentPageNo == 1) {
                    mRvMineRed.refreshComplete();
                } else {
                    mRvMineRed.loadMoreComplete();
                }
                LogUtil.i(errorMsg + "statusCode" + statusCode);
                Toast.makeText(getActivity(), errorMsg, Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
